/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.webos.java.explorer;

import org.json.JSONArray;
import org.json.JSONObject;
import org.webos.java.base.Frame;
import org.webos.java.base.FrameType;
import org.webos.java.base.RemoteMessage;
import org.webos.java.base.fs.FileDescriptor;
import org.webos.java.base.fs.FileManager;
import org.webos.java.base.fs.FileType;
import org.webos.java.base.workspace.Workspace;
import org.webos.java.root.Log;
import org.webos.java.base.NetworkManager;

/**
 *
 * @author Kostya
 */

@FrameType(code="file-viewer", jsClass="explorer.ViewPanel")
public class FileViewPanel extends Frame{

    private Explorer explorer = null;
    private FileTreePanel2 treePanel = null;
    private FileDescriptor current = null;
    private FileType currentFileType = null;

    public FileViewPanel(Explorer explorer, Workspace ws) {
        super(explorer, ws);
        this.explorer = explorer;
        setTitle("No file selected");
    }

    void setTreePanel(FileTreePanel2 treePanel) {
        this.treePanel = treePanel;
    }

    @RemoteMessage
    public JSONObject messageToViewer(JSONObject aData){
        try {
            if(current!=null){
                FileType fileType = FileManager.getFileType(current.getType());
                if(fileType!=null)
                    return fileType.messageFromViewer(aData, current, explorer);
            }
        } catch (Throwable t) {
            Log.error(this, "Exception thrown: " + t.getMessage());
            t.printStackTrace();
        }
        return NetworkManager.getErrorObject(8, "Can't find file");
    }

    @RemoteMessage
    public JSONObject editFile(JSONObject aData){
        if(current==null)
            return NetworkManager.getErrorObject(4, "Can't find file");
        return treePanel.editFile(current, currentFileType);
    }

    @RemoteMessage
    public JSONObject removeFile(JSONObject aData){
        if(current==null)
            return NetworkManager.getErrorObject(4, "Can't find file");
        return treePanel.removeFile(current);
    }

    public void fileUpdated(FileDescriptor aDescriptor, boolean aNewFile){

    };
    
    @RemoteMessage
    public void refreshPanel(JSONObject aData){
        displayFile(current, null);
    }

    @RemoteMessage
    public void openAs(JSONObject aData){
        displayFile(current, FileManager.getFileType(aData.optString("type", "")));
    }

    protected void displayFile(FileDescriptor aFile, FileType aSpecialType){
        try {
            JSONObject fileInfo = new JSONObject();
            current = aFile;
            if(aFile==null){
                fileInfo.put("name", "No file selected");
                fileInfo.put("editable", false);
                fileInfo.put("removable", false);
                fileInfo.put("reloadable", false);
                currentFileType = null;
            }else{
                fileInfo.put("id", aFile.getId());
                fileInfo.put("name", aFile.getName());
                fileInfo.put("editable", aFile.isEditable());
                fileInfo.put("removable", aFile.isRemovable());
                fileInfo.put("reloadable", true);
                currentFileType = aSpecialType;
                if(currentFileType==null)
                    currentFileType = FileManager.getFileType(aFile.getType());
                boolean plainFile = true;
                if(currentFileType!=null){
                    fileInfo.put("viewer_class", currentFileType.getViewerClass(aFile));
                    fileInfo.put("viewer_object", currentFileType.getViewer(aFile));
                    if(!currentFileType.isPlainFile())
                        plainFile = false;
                }else{
                    fileInfo.put("editable", false);
                }
                if(plainFile){
                    String[] plainFileTypes = FileManager.getPlainFileTypes();
                    JSONArray arr = new JSONArray();
                    for (String fType : plainFileTypes) {
                        FileType ft = FileManager.getFileType(fType);
                        if(ft==null)
                            continue;
                        JSONObject obj = new JSONObject();
                        obj.putOpt("id", fType);
                        obj.putOpt("icon", ft.getIcon());
                        obj.putOpt("name", ft.getDescription());
                        arr.put(obj);
                    }
                    fileInfo.putOpt("open_as", arr);
                }
                setIcon(aFile.getIcon());

            }
            setTitle("Preview: "+fileInfo.getString("name"));
            sendMessage("displayFile", fileInfo);
        } catch (Throwable t) {
            Log.error(this, "Exception thrown: " + t.getMessage());
            t.printStackTrace();
        }
    }

}
